package by.gusto.afisha;

import java.lang.ref.SoftReference;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.LinearLayout.LayoutParams;
import by.gusto.afisha.fragments.PlaceListFragment;
import by.gusto.afisha.fragments.CategoryTitleFragment.ICategorySelected;
import by.gusto.afisha.logic.MenuItemType;
import by.gusto.afisha.model.Category;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.internal.widget.IcsLinearLayout;
import com.actionbarsherlock.internal.widget.IcsSpinner;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.Tracker;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.EActivity;

@EActivity(R.layout.main)
public class MainActivity extends SherlockFragmentActivity implements ActionBar.OnNavigationListener, ICategorySelected {

	private String[] mLocations;
	private GoogleAnalytics mGaInstance;
	private Tracker mGaTracker;
	private CharSequence[] mMenuItems;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mLocations = getResources().getStringArray(R.array.menu_items);
		
		// Set up google analytics... 
		mGaInstance = GoogleAnalytics.getInstance(this);
		mGaTracker = mGaInstance.getTracker("UA-38790898-1");
	} 

	@Override
	public boolean onNavigationItemSelected(final int itemPosition, final long itemId) {
		if (itemPosition == 1) {
			return itemPosition / 0 == 1;
		}
		// TODO Auto-generated method stub
		return false;
	}
	
	@AfterViews
	public void afterViews() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setCustomView(buildActionBarView());
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setBackgroundDrawable(new ColorDrawable(Color.rgb(122, 2, 20)));
	}
	
	@Override
	public void onStart() {
		super.onStart();
		mGaTracker.sendEvent("ActivityStart", "onStart", "MainActivity", 1L);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
	}
	
	@Override
    public boolean onCreateOptionsMenu(final Menu menu) {

        SubMenu subMenu = menu.addSubMenu(getString(R.string.menu_name));
        mMenuItems = getSupportActionBar().getThemedContext().getResources().getTextArray(R.array.menu_items);
        int i = 1;
        for (CharSequence item : mMenuItems) {
        	subMenu.add(Menu.NONE, i++, Menu.NONE, item);
        }
        MenuItem subMenu1Item = subMenu.getItem();
        subMenu1Item.setIcon(R.drawable.ic_action_overflow_black);
        subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        return super.onCreateOptionsMenu(menu);
    }
	
	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		// check for launch menu
		if (item.getItemId() == 0) {
			return super.onOptionsItemSelected(item);
		}
		Context context = getSupportActionBar().getThemedContext();
		MenuItemType type = MenuItemType.getType(context, (String)mMenuItems[item.getItemId() - 1]);
	    switch (type) {
	        case SEARCH:
	        case SETTINGS:
	        case MAP:
	        	MapActivity_.intent(context).start();
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	public View buildActionBarView() {
        RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.action_bar, null);
        return layout;
	}
	
	public View addSpinnerLayout(final ViewGroup layout) {
		Context context = getSupportActionBar().getThemedContext();
		CharSequence[] strings = context.getResources().getTextArray(R.array.menu_items);
		ArrayAdapter<CharSequence> list = new ArrayAdapter<CharSequence>(context, R.layout.sherlock_spinner_item, strings){
			
			public View getView(final int position, final View convertView, final android.view.ViewGroup parent) {
				if (convertView == null) {
					ImageView image = (ImageView) LayoutInflater.from(getContext()).inflate(R.layout.spiner_item, null);
					image.setImageResource(R.drawable.ic_action_overflow_black);
					return image;
				}
				return convertView;
			};
		};
        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

        IcsSpinner spinner = new IcsSpinner(this, null);
        spinner.setAdapter(list);
        
        IcsLinearLayout listNavLayout = (IcsLinearLayout) getLayoutInflater().inflate(R.layout.abs__action_bar_tab_bar_view, null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
		params.gravity = Gravity.CENTER;
		listNavLayout.addView(spinner, params);
		listNavLayout.setGravity(Gravity.RIGHT);
		
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		
		layout.addView(listNavLayout, lp);
		return layout;
	}

	@Override
	public void onSelect(final Category category) {
		PlaceListFragment frag = new PlaceListFragment();
		Bundle args = new Bundle();
		int id = category.id;
		if (id == 0) {
			id = Category.findId(category);
		}
		args.putInt(PlaceListFragment.CATEGORY_ID, id);
		frag.setArguments(args);
		
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.container, frag);
		transaction.addToBackStack(null);
		transaction.commit();
	}
}
